package com.photoeditor.demo.model.image.collage.util;


import android.graphics.RectF;

import com.photoeditor.demo.model.image.collage.templet.Line;


public class MathUtil {
	/**
	 * 三个点获取角度
	 * 这里获取的是x1,y1位置的角  即角a
	 * 注意在这里 x1,y1传入的中心点，x2,y2传入的点击的点，x3,y3是一个假定的点
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @param x3
	 * @param y3
	 * @return
	 */
	public static double getDegree(float x1, float y1, float x2, float y2, float x3, float y3){
		double a = Math.sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
		double b = Math.sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1));
		double c = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
		
		/*余弦定理!
		余弦定理是揭示三角形边角关系的重要定理,直接运用它可解决一类已知三角形两边及夹角求第三边或者是已知三个边求角的问题,若对余弦定理加以变形并适当移于其它知识,则使用起来更为方便、灵活.
		对于任意三角形 三边为a,b,c 三角为A,B,C 满足性质 
		a^2=b^2+c^2-2*b*c*CosA 
		b^2=a^2+c^2-2*a*c*CosB 
		c^2=a^2+b^2-2*a*b*CosC 
		CosC=(a^2+b^2-c^2)/2ab 
		CosB=(a^2+c^2-b^2)/2ac 
		CosA=(c^2+b^2-a^2)/2bc*/
		/**
		 * i是通过余弦定理计算出来的CosA
		 */
		double i = (b * b + c * c - a * a) / (2 * b * c);
		
		/**
		 * 做保护
		 */
		i = Math.min(i, 1);
		i = Math.max(i, -1);
		
		/**
		 * 通过反余弦算出角度
		 */
		return Math.toDegrees(Math.acos(i));
	}
	
	/**
	 * 获取当前点所在的象限
	 * @param x
	 * @param y
	 * @return
	 */
	public static int getLocation(float x, float y){
		if(x > 0 && y <= 0){
			return 1;
		} else if(x <= 0 && y < 0){
			return 2;
		} else if(x < 0 && y >= 0){
			return 3;
		} else if(x >= 0 && y > 0){
			return 4;
		}
		return 0;
	}

	/**
	 * 用于计算微旋转后如果想截取原来的大小所需要的缩放
	 * @param rotation
	 * @param w
	 * @param h
	 * @return
	 */
	public static float getCurrentScale(float rotation, float w, float h){
		if(w <= h){
			return (float)(Math.sqrt(Math.pow(w, 2) + Math.pow(h, 2)) * Math.cos(Math.atan(h / w) - Math.toRadians(Math.abs(rotation)))) / w;
		} else{
			return (float)(Math.sqrt(Math.pow(w, 2) + Math.pow(h, 2)) * Math.cos(Math.toRadians(90) - Math.atan(h / w) - Math.toRadians(Math.abs(rotation)))) / h;
		}
	}


	/**
	 * 获取当前点和原点连成的直线  在 X逆时针旋转的角度值
	 * @param line
	 * @param location
	 * @return
	 */
	public static double getDegreeFromX(Line line, int location){
		double degree = 0;
		if(line.isVertical() || line.isHrizontal()) {
			if (location == 1) {
			} else if (location == 2) {
				degree += 90;
			} else if (location == 3) {
				degree += 180;
			} else if (location == 4) {
				degree += 270;
			}
		} else{
			degree = Math.abs(Math.toDegrees(Math.atan(line.getK())));// 0 - 90
			if (location == 1) {
			} else if (location == 2) {
				degree = 180 - degree;
			} else if (location == 3) {
				degree += 180;
			} else if (location == 4) {
				degree = 360 - degree;
			}
		}
		return degree;
	}

	/**
	 * 获取能包含这四个点的最小矩阵
	 * @param point1
	 * @param point2
	 * @param point3
	 * @param point4
     * @return
     */
	public static RectF point2RectF(float[] point1, float[] point2, float[] point3, float[] point4){
		RectF rectF = new RectF();
		rectF.set(point1[0], point1[1], point1[0], point1[1]);
		rectF.union(point2[0], point2[1]);
		rectF.union(point3[0], point3[1]);
		rectF.union(point4[0], point4[1]);
		return rectF;
	}
}
